import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_kit_dsp/flutter_kit_dsp.dart';

import '../../tool/assets.dart';

class SoundItem extends StatefulWidget {
  ChannelModel ch;

  SoundItem({super.key, required this.ch});

  @override
  State<SoundItem> createState() => SoundItemState();
}

class SoundItemState extends State<SoundItem> {
  final _textEditingController = TextEditingController();

  @override
  void dispose() {
    _textEditingController.dispose();
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
    _textEditingController.text = '';
    _textEditingController.addListener(() {
      String text = _textEditingController.text;
      // 自动添加前导0
      if (text.isNotEmpty && text[0] == '.') {
        _textEditingController.text = '0$text';
        return;
      } else if (text.length > 1 && text[0] == '0' && text[1] != '.') {
        _textEditingController.text = text.substring(1, text.length);
        return;
      }
    });
  }

  OutlineInputBorder _border({bool hight = false}) {
    return OutlineInputBorder(
      borderRadius: BorderRadius.circular(4),
      borderSide: BorderSide(color: hight ? Color_select_btn : Color_border, width: hight ? 1.5 : 1.0),
    );
  }

  ///输入框
  Widget _item2() {
    return SizedBox(
      height: 20,
      child: TextField(
        textAlignVertical: TextAlignVertical.center,
        textAlign: TextAlign.center,
        cursorHeight: 10, // 设置光标高度
        cursorColor: Color_select_btn, //设置光标颜色
        style: const TextStyle(fontSize: 12),
        controller: _textEditingController,
        keyboardType: const TextInputType.numberWithOptions(decimal: true),

        ///输入规则限制
        inputFormatters: [
          LengthLimitingTextInputFormatter(5), // 限制输入最大长度为3
          //FilteringTextInputFormatter.digitsOnly, // 只允许输入数字
          FilteringTextInputFormatter(
            RegExp(r'^\d*\.?\d*$'), // 允许数字和一个小数点
            allow: true,
          ),
        ],

        ///响应方法
        onTap: () => globalController.beginDelayEdit(widget.ch),
        onChanged: (value) => globalController.updateDelayEdit(widget.ch, double.parse(value)),
        onSubmitted: (value) {
          FocusScope.of(context).unfocus();
          globalController.endDelayEdit(widget.ch);
        },
        onEditingComplete: () => globalController.endDelayEdit(widget.ch),

        ///边框设置
        decoration: InputDecoration(
          isDense: true, // 密集模式，减少内部空间
          contentPadding: EdgeInsets.zero, // 取消内边距
          filled: true, //启动背景颜色
          fillColor: Color_bg_btn, //背景颜色
          constraints: const BoxConstraints(
            maxWidth: 40,
            maxHeight: 30,
          ),

          ///默认边框
          border: _border(),

          ///焦点边框
          focusedBorder: _border(hight: true),

          ///未获取焦点边框
          enabledBorder: _border(),

          ///错误边框
          errorBorder: _border(),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    _textEditingController.text = globalController.autoDelay(widget.ch);

    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      spacing: 10,
      children: [
        InkWell(
          splashColor: Colors.transparent,
          highlightColor: Colors.transparent,
          onTap: () => globalController.beginDelayEdit(widget.ch),
          child: Image.asset(widget.ch.isDelayEdit ? widget.ch.sound_img_select : widget.ch.sound_img,
              width: 35, height: 35, alignment: Alignment.bottomCenter, fit: BoxFit.scaleDown),
        ),
        const SizedBox(height: 10),
        _item2(),
      ],
    );
  }
}
